package com.lxl.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

/**
 * 贴吧
 * 
 * @author 刘星鹭
 * @Email liuxinglumail@163.com
 * @version 创建时间： 2014年8月31日 下午7:08:06
 */

@Entity
@Table(name = "t_tiebas")
public class Tieba {
    private String tiebaName;// 贴吧名
    private int deleted = 0;// 是否被删除，0为未删除
    private Set<User> thisTiebasUsers = new HashSet<User>(); // 本贴吧的用户

    /**
     * 无参构造方法
     */
    public Tieba() {
    }

    /**
     * 带参构造方法
     * 
     * @param tiebaName
     *            贴吧名
     */
    public Tieba(String tiebaName) {
        this.tiebaName = tiebaName;
    }

    /**
     * 获得贴吧名
     * 
     * @return 贴吧名
     */
    @Id
    // 主键
    @Column(name = "tieba_name", length = 20, nullable = false)
    // 字段长度为20，不可为空
    public String getTiebaName() { // 以下均为Getter和Setter
        return tiebaName;
    }

    /**
     * 设置贴吧名
     * 
     * @param tiebaName
     *            贴吧名
     */
    public void setTiebaName(String tiebaName) {
        this.tiebaName = tiebaName;
    }

    /**
     * 获得该贴吧所有用户
     * 
     * @return 该贴吧所有用户
     */
    @ManyToMany(cascade = {CascadeType.REFRESH, CascadeType.MERGE} // 笔记：级联删除基本用不到
    , mappedBy = "favourateTiebas")
    public Set<User> getThisTiebasUsers() {
        return thisTiebasUsers;
    }

    /**
     * 设置该贴吧所有用户
     * 
     * @param thisTiebasUsers
     *            该贴吧所有用户
     */
    public void setThisTiebasUsers(Set<User> thisTiebasUsers) {
        this.thisTiebasUsers = thisTiebasUsers;
    }

    /**
     * 判断该贴吧是否被删除
     * 
     * @return 该贴吧是否被删除
     */
    @Column(name = "deleted", length = 1, nullable = false)
    // 字段长度为20，不可为空
    public int getDeleted() {
        return deleted;
    }

    /**
     * 设置该贴吧是否被删除
     * 
     * @param deleted
     *            该贴吧是否被删除
     */
    public void setDeleted(int deleted) {
        this.deleted = deleted;
    }

    // 重写hashcode和equals方法，因为对象是否相等是用tiebaName判断的，生成方法是alt+shift+s生成hashcode()和equals()
    /**
     * hashCode方法
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((tiebaName == null) ? 0 : tiebaName.hashCode());
        return result;
    }

    /**
     * equals方法
     */
    @Override
    public boolean equals(Object obj) {

        if (this == obj) {
            return true;
        } else if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        Tieba other = (Tieba) obj;
        if (tiebaName == null || !tiebaName.equals(other.tiebaName)
                && other.tiebaName != null) {
            return false;
        }
        return true;
    }
}
